package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CheckDao;
import dao.impl.ICheckDao;
import entity.User;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
		
			User u = new User();
			u.setUsername(username);
			u.setPassword(password);
			
			CheckDao cd = new ICheckDao();
			boolean flag = cd.checklogin(u);
			String msg = "<response>";
			if (flag) {
				msg+="<result>success</result>";
				request.getSession().setAttribute("username", username);
			}else{
				msg+= "<result>用户名密码错误</result>";
			}
		
			msg+="</response>";
		
			response.setContentType("text/xml;charset=UTF-8");
			//设置响应对象中的缓存是否启用
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter out = response.getWriter();
			out.println(msg);
			out.close();
	}

}
